﻿/*
** 课程首页(Class_Index)
*/
$(function () {
    var $menuLi = $('#nav .menu');
    var $menuHd = $('#nav .menu-hd');
    var $menuBd = $('#nav .menu-bd');
    var $arrow = $('#nav .m1 i');
    $menuBd.hover(function () {
        $menuHd.addClass('on');
    }, function () {
        $menuHd.removeClass('on');
    });
    $menuLi.hover(function () {
        $menuHd.children('b').addClass('b-on');
    }, function () {
        $menuHd.children('b').removeClass('b-on');
    });

    //价格
    var item= function(itemName,itemValue) {
        this.itemName = itemName;
        this.itemValue = itemValue;
    }
    
    function ClassIndex() {
        var self = this;
        self.catelogid=ko.observable(0);
        self.table = ko.observableArray([]);
        self.price = ko.observableArray([
            new item("全部", 0),
            new item("免费", 1),
            new item("付费", 2)
        ]);
        self.sort = ko.observableArray([
          new item("按最新", 0),
          new item("按最热", 1),
        ]);
        self.priceSelect = ko.observable(0);
        self.sortSelect = ko.observable(0);
        self.pageSize = 10;
        self.page = new common.Page(self.pageSize, self.pageSize, 1, self.CallBack);
        
        self.changeSelect = ko.computed(function () {
            if (self.priceSelect() && self.sortSelect()) {
                self.CallBack(self.pageSize, 1);
            }
        })
    }
     

        
    //分页回调函数
    ClassIndex.prototype.CallBack = function (pageSize, pageIndex) {
        var selectData = {
            pagesize: pageSize,
            currentpage: pageIndex,
            free: viewModel.priceSelect().itemValue,
            catelog: viewModel.catelogid(),
            neworhot: viewModel.sortSelect().itemValue
        };
        common.ajax(function (data) {
            viewModel.page.totalCount(data.Count);
            viewModel.page.currentPage(pageIndex);
            viewModel.table(data.CourseInfos);
        }, null, "GetClassInfo", selectData, 'GET');
        
        
    }
    
    var viewModel = new ClassIndex();
    
    

    ko.applyBindings(viewModel);
    
    viewModel.CallBack(viewModel.pageSize, 1);


    $(".selectCatelog").live("click", function() {
        var id = $(this).attr("rel");
        var name = $(this).text();
        $("#catelogName").html(name);
        viewModel.catelogid(id);
        viewModel.CallBack(viewModel.pageSize, 1);
    });

    $("#ulclass").find("li").live("click", function () {
        var id = $(this).find("input").val();
        location.href = "/CloudClass/Class/ClassPage/"+id;
    });

})